﻿using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Profinet.Toledo
{
	/// <summary>
	/// 托利多电子秤的TCP服务器，启动服务器后，等待电子秤的数据连接。
	/// </summary>
	// Token: 0x02000047 RID: 71
	public class ToledoTcpServer : NetworkServerBase
	{
		/// <summary>
		/// 获取或设置当前的报文否是含有校验的，默认为含有校验
		/// </summary>
		// Token: 0x17000141 RID: 321
		// (get) Token: 0x060005EB RID: 1515 RVA: 0x0001B210 File Offset: 0x00019410
		// (set) Token: 0x060005EC RID: 1516 RVA: 0x0001B218 File Offset: 0x00019418
		public bool HasChk { get; set; } = true;

		/// <inheritdoc />
		// Token: 0x060005ED RID: 1517 RVA: 0x0001B224 File Offset: 0x00019424
		protected override void ThreadPoolLogin(Socket socket, IPEndPoint endPoint)
		{
			AppSession appSession = new AppSession();
			appSession.WorkSocket = socket;
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), string.Format(StringResources.Language.ClientOnlineInfo, appSession.IpEndPoint));
			}
			bool flag = !appSession.WorkSocket.BeginReceiveResult(new AsyncCallback(this.ReceiveCallBack), appSession).IsSuccess;
			if (flag)
			{
				ILogNet logNet2 = base.LogNet;
				if (logNet2 != null)
				{
					logNet2.WriteError(this.ToString(), StringResources.Language.NetClientLoginFailed);
				}
			}
		}

		// Token: 0x060005EE RID: 1518 RVA: 0x0001B2B4 File Offset: 0x000194B4
		[DebuggerStepThrough]
		private void ReceiveCallBack(IAsyncResult ar)
		{
			ToledoTcpServer.<ReceiveCallBack>d__6 <ReceiveCallBack>d__ = new ToledoTcpServer.<ReceiveCallBack>d__6();
			<ReceiveCallBack>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
			<ReceiveCallBack>d__.<>4__this = this;
			<ReceiveCallBack>d__.ar = ar;
			<ReceiveCallBack>d__.<>1__state = -1;
			<ReceiveCallBack>d__.<>t__builder.Start<ToledoTcpServer.<ReceiveCallBack>d__6>(ref <ReceiveCallBack>d__);
		}

		/// <summary>
		/// 当接收到一条新的托利多的数据的时候触发
		/// </summary>
		// Token: 0x14000009 RID: 9
		// (add) Token: 0x060005EF RID: 1519 RVA: 0x0001B2F4 File Offset: 0x000194F4
		// (remove) Token: 0x060005F0 RID: 1520 RVA: 0x0001B32C File Offset: 0x0001952C
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event ToledoTcpServer.ToledoStandardDataReceivedDelegate OnToledoStandardDataReceived;

		/// <inheritdoc />
		// Token: 0x060005F1 RID: 1521 RVA: 0x0001B361 File Offset: 0x00019561
		public override string ToString()
		{
			return string.Format("ToledoTcpServer[{0}]", base.Port);
		}

		/// <summary>
		/// 托利多数据接收时的委托
		/// </summary>
		/// <param name="sender">数据发送对象</param>
		/// <param name="toledoStandardData">数据对象</param>
		// Token: 0x02000289 RID: 649
		// (Invoke) Token: 0x06002415 RID: 9237
		public delegate void ToledoStandardDataReceivedDelegate(object sender, ToledoStandardData toledoStandardData);
	}
}
